
var path = require('path')
var webpack = require('webpack')
var px2rem = require('postcss-plugins-px2rem');
const ExtractTextPlugin=require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin')
var CopyWebpackPlugin=require('copy-webpack-plugin')
var htmlwebpack=require('./src/config/htmlwebpack')


module.exports = {
    entry:{
        index:'./src/js/index',
        login:'./src/js/login',
        change_password:'./src/js/change_password',
    },
    output: {
        path: path.join(__dirname, './dist'),
/*        publicPath: "",*/
        filename: '[name].7b232.js',
        chunkFilename:'[name].7b232.js',
        //异步加载的模块以文件形式加载，生成的文件名以chunkFilename配置
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options:{
                    loaders:{    //加入对vue行内样式写法的兼容，需要将vue-loader的版本升级到10.0以上
                        scss: 'style-loader!css-loader!postcss-loader!sass-loader',
                        sass: 'style-loader!css-loader!postcss-loader!sass-loader?indentedSyntax',
                    }
                }
            },
            {
                test: /\.css$/,
/*                exclude: /node_modules/,*/
                loader:ExtractTextPlugin.extract({fallback:'style-loader',use:'css-loader'})
            },
            {
                test: /\.sass$/,
                exclude: /node_modules/,
                loader:ExtractTextPlugin.extract({fallback:'style-loader',use:'css-loader!postcss-loader!sass-loader'})
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
               loader:ExtractTextPlugin.extract({fallback:'style-loader',use:'css-loader!postcss-loader!sass-loader'})

            },
            {
                test: /\.less$/,
                exclude: /node_modules/,
                loader: 'style-loader!css-loader!postcss-loader!less-loader'
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            // 字体
            {
                test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
                loaders: ['url-loader']
            },
            {
                test: /\.json$/, exclude: /node_modules/,
                loaders: ['url-loader?limit=100000&name=[name].[ext]']
            },
            { test: /\.(mp3)(\?.*)?$/, loader: 'url-loader'},
        ]
    },

    plugins: [
        new webpack.LoaderOptionsPlugin({
            options: {
                // postcss: [px2rem({
                //       remUnit:16 ,
                //       baseDpr:1,
                //       remPrecision:14
                //   })]
            },
        }),
        require('autoprefixer'),
        new ExtractTextPlugin('[name].7b232.css'),
        new CleanWebpackPlugin(['dist']),
        //复制html errors 404文件到dist
        new CopyWebpackPlugin([{
            from:__dirname+'/src/html/errors',
            to:__dirname+'/dist/errors',
        }])
    ].concat(htmlwebpack),
    resolve: {
        extensions: ['.js', '.vue','.scss'],
        alias: {
            com:path.join(__dirname,'./src/common'),
            c: path.join(__dirname, './src/component'),
             js: path.join(__dirname, './src/js'),
            page:path.join(__dirname, './src/page'),
            css:path.join(__dirname, './src/css'),
            'vue$': 'vue/dist/vue.common.js',
        }

    },
    devServer: {
        historyApiFallback: false,
        noInfo: true,

    },
    devtool: '#source-map'
}



if (process.env.NODE_ENV === 'production') {
    module.exports.devtool = '#source-map'
    // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.NoErrorsPlugin(),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"'+process.env.NODE_ENV+'"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true,
        })
    ])
}else  if(process.env.NODE_ENV==='development'){
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"development"'
            }
        }),
/*        new webpack.HotModuleReplacementPlugin()*/
       ]);
}

